@tool
class_name Skill_Control_Repeat
extends SkillControl

## Repeat 重复
## 重复repeat_times次，可以中途Stop
#region 属性
## 负数表示无限次
@export_range(-1,100000) var repeat_times:int = 1
## 是否在返回Failure时Stop
@export var is_stop_by_failure:bool = false
var cur_repeat_times:int = 0
#endregion
#region 字段
#endregion
#region 信号
#endregion

#region 基类方法
## 初始化
func init(action_idx:int = -1,init_data:Dictionary = {},handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
	super.init(action_idx,init_data,self_handle_func)
## 释放
func release(args:Dictionary = {},handle_func:Callable = Callable()):
	var self_handle_func = func():
		cur_repeat_times = 0
		if handle_func.is_valid():
			handle_func.call()
		for action in sub_actions:
			action.release(args)
	super.release(args,self_handle_func)
## 打断
func interrupt(handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		for action in sub_actions:
			action.interrupt()
	super.interrupt(self_handle_func)
## 完成
func complete(release_result:GameEnum.SkillReleaseResult = GameEnum.SkillReleaseResult.Success,handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		for action in sub_actions:
			action.complete()
	super.complete(release_result,self_handle_func)
## 冷却
func cool(handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		for action in sub_actions:
			action.cool()
	super.cool(self_handle_func)
## 冷却完成
func cool_complete(handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		for action in sub_actions:
			action.cool_complete()
	super.cool_complete(self_handle_func)
## 重置冷却
func reset_cool(handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		for action in sub_actions:
			action.reset_cool()
	super.reset_cool(self_handle_func)
## 销毁
func destroy(handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		for action in sub_actions:
			action.destroy()
	super.destroy(self_handle_func)
## 判断该技能能否释放
func check_can_release() -> GameEnum.SkillReleaseStatus:
	var res = super.check_can_release()
	if res != GameEnum.SkillReleaseStatus.Success:
		return res
	return GameEnum.SkillReleaseStatus.Success
func on_sub_action_release_completed(skill_action:SkillAction,release_result:GameEnum.SkillReleaseResult):
	if not is_releasing:
		return
	cur_repeat_times += 1
	if is_stop_by_failure and release_result == GameEnum.SkillReleaseResult.Failure:
		result = GameEnum.SkillReleaseResult.Failure
		is_completed = true
	if cur_repeat_times >= repeat_times and repeat_times > 0:
		result = GameEnum.SkillReleaseResult.Success
		is_completed = true
	if check_is_completed():
		complete(result)
	else:
		for action in sub_actions:
			if not action.is_can_release:
				await action.skill_action_cool_completed
			action.release(release_args)
func on_sub_action_interrupted(skill_action:SkillAction):
	if not is_releasing:
		return
	cur_repeat_times += 1
	if repeat_times > 0 and cur_repeat_times >= repeat_times:
		result = GameEnum.SkillReleaseResult.Success
		is_completed = true
	if check_is_completed():
		complete(result)
	else:
		for action in sub_actions:
			if not action.is_can_release:
				await action.skill_action_cool_completed
			action.release(release_args)
#endregion
#region 公共方法
#endregion
#region 私有方法
#endregion
#region 生命周期函数
func _get_configuration_warnings() -> PackedStringArray:
	var warnings = super._get_configuration_warnings()
	if get_child_count() > 1:
		warnings.append("此节点最多只能有一个节点")
	return warnings
#endregion
